﻿using System;

namespace Library.OrmEnterpriseEntity.TableInfo {
	///<summary>
	///</summary>
	public class ThrowValidationExceptionEventArgs : EventArgs {
		///<summary>
		/// ThrowValidationExceptionEventArgs contructor for one column
		///</summary>
		///<param name="column"></param>
		///<param name="error"></param>
		public ThrowValidationExceptionEventArgs(DbColumn column, ValidationError error)
			: this(new[] { column }, error) {
		}

		///<summary>
		/// ThrowValidationExceptionEventArgs contructor for many columns
		///</summary>
		///<param name="columns">Columns cause validating error</param>
		///<param name="error">Error Type</param>
		///<param name="tableError">Table cause validating error</param>
		public ThrowValidationExceptionEventArgs(DbColumn[] columns, ValidationError error, DbTableView tableError) {
			TableError = tableError;
			Columns = columns;
			ErrorType = error;
			if (columns != null) {
				ColumnNames = new string[columns.Length];
				ColumnValues = new object[columns.Length];
				TableNames = new string[columns.Length];
				for (int i = 0; i < columns.Length; i++) {
					ColumnNames[i] = columns[i].ColumnName;
					ColumnValues[i] = columns[i].DbValue;
					TableNames[i] = columns[i].Table.TableName;
				}
			}
		}
		///<summary>
		/// ThrowValidationExceptionEventArgs contructor for many columns
		///</summary>
		///<param name="columns">Columns cause validating error</param>
		///<param name="error">Error Type</param>
		public ThrowValidationExceptionEventArgs(DbColumn[] columns, ValidationError error)
			: this(columns, error, null) {
			if (columns != null && columns.Length > 0) {
				TableError = columns[0].TableView;
			}
		}
		///<summary>
		/// Cultural message for validation exception
		///</summary>
		public string Message { get; set; }
		///<summary>
		/// Caption of columns
		///</summary>
		public string[] ColumnNames { get; set; }
		/// <summary>
		/// First column name
		/// </summary>
		public string ColumnName {
			get {
				if (ColumnNames != null && ColumnNames.Length > 0) return ColumnNames[0];
				return null;
			}
		}
		///<summary>
		/// Table names
		///</summary>
		public string[] TableNames { get; set; }
		/// <summary>
		/// First table name
		/// </summary>
		public string TableName {
			get {
				if (TableNames != null && TableNames.Length > 0) return TableNames[0];
				return null;
			}
		}
		///<summary>
		///</summary>
		public object[] ColumnValues { get; set; }
		/// <summary>
		/// First column value
		/// </summary>
		public object ColumnValue {
			get {
				if (ColumnValues != null && ColumnValues.Length > 0) return ColumnValues[0];
				return null;
			}
		}
		///<summary>
		/// Columns cause validating error
		///</summary>
		public DbColumn[] Columns { get; private set; }
		///<summary>
		/// Error type
		///</summary>
		public ValidationError ErrorType { get; private set; }
		/// <summary>
		/// Table cause validate error
		/// </summary>
		public DbTableView TableError { get; private set; }
		/// <summary>
		/// Tag
		/// </summary>
		public object Tag { get; set; }
	}
}
